package main

import (
	"gitee.com/zhanghuamingmyself/api/device"
	"gitee.com/zhanghuamingmyself/hook"
	"gitee.com/zhanghuamingmyself/hook/bean"
	"gitee.com/zhanghuamingmyself/util"
	"log"
	"time"
)

func main() {
	TestHook()
	//TestApi()
}

func TestHook() {
	go func() {
		hook.Connect("test", func(msg bean.SystemHookWarp) error {
			log.Println(msg)
			return nil
		})
	}()

	for true {
		time.Sleep(10000000)
	}
}

func TestApi() {
	util.Token = "${token}"
	listDevice()
}

func listDevice() {
	req := device.IotDeviceListReq{"ONLINE", nil}
	deviceList, err := device.ListDevice(req)
	if nil == err {
		for i, item := range deviceList {
			if i == 0 {
				//sendSetting(item.DeviceCore)
				//listSensor(item.DeviceCore)
				//listExplain(item.DeviceCore)
				//listParam(item.DeviceCore)
				listSetting(item.DeviceCore)
			}
		}
	}
}

func listSensor(deviceCore string) {
	req := device.IotDeviceSensorListReq{}
	req.DeviceCore = deviceCore
	var withParam =true
	req.WithParam = &withParam
	var withExplain =true
	req.WithExplain = &withExplain
	var withSetting =true
	req.WithSetting = &withSetting
	sensorList, err := device.ListDeviceSensor(req)
	if nil == err {
		for i, item := range sensorList {
			if i == 0 {
				listSensorDataWithExplain(deviceCore, item.Port)
			}
			//log.Println(item)
		}
	}
}

func listSensorData(deviceCore string, port string) {
	endTime := time.Now().Unix()
	startTime := endTime - (60 * 60 * 12)
	req := device.IotDeviceSensorDataListReq{}
	req.DeviceCore = deviceCore
	req.Port = port
	req.EndTime = endTime
	req.StartTime = startTime
	dataList, err := device.ListDeviceSensorData(req)
	if nil == err {
		for _, item := range dataList {
			log.Println(item)
		}
	}
}

func listSensorDataWithExplain(deviceCore string, port string) {
	endTime := time.Now().Unix()
	startTime := endTime - (60 * 60 * 12)
	req := device.IotDeviceSensorDataListReq{}
	req.DeviceCore = deviceCore
	req.Port = port
	req.EndTime = endTime
	req.StartTime = startTime
	res, err := device.ListDeviceSensorDataWithExplain(req)
	if nil == err {
		for _, item := range res.DataList {
			log.Println(item)
		}
	}
}

func listExplain(deviceCore string) {
	req := device.IotDeviceExplainListReq{}
	req.DeviceCore = deviceCore
	explainList, err := device.ListDeviceExplain(req)
	if nil == err {
		for _, item := range explainList {
			log.Println(item)
		}
	}
}

func listParam(deviceCore string) {
	req := device.IotDeviceParamListReq{}
	req.DeviceCore = deviceCore
	paramList, err := device.ListDeviceParam(req)
	if nil == err {
		for _, item := range paramList {
			log.Println(item)
		}
	}
}

func listSetting(deviceCore string) {
	req := device.IotDeviceSettingListReq{}
	req.DeviceCore = deviceCore
	settingList, err := device.ListDeviceSetting(req)
	if nil == err {
		for _, item := range settingList {
			log.Println(item)
		}
	}
}

func sendSetting(deviceCore string) {
	req := device.IotDeviceSettingSendReq{}
	req.DeviceCore = deviceCore
	req.KeyName = "getData"
	req.Port = "0"
	req.Value = "null"
	var outTime int32 = 10
	req.OutTime = &outTime
	res, err := device.SendDeviceSetting(req)
	if nil == err {
		log.Println(res)
	} else {
		log.Fatal(err)
	}
}
